from model.University import University
from model.UniversityScoreRankMean import UniversityScoreRankMean


# 批量处理院校名称后面带数字，学校名称后面带有（精准专项）的名字，还是显示（精准脱贫）
def to_do():
    universityScoreRankMean = UniversityScoreRankMean()
    year = 2023
    data = universityScoreRankMean.getList(year)
    for index, row in enumerate(data):
        print(index, row)
        if row['name']:
            update = dict()
            update['id'] = row['id']
            name = row['name']
            name = name.replace(" ", "")
            # 去掉右边的数字
            if name[-1].isdigit():
                d = name[len(name.rstrip('0123456789')):]
                name = name.split(d)[0]
            # 去掉字符串所有空格 院校名称是否有 精准脱贫 ，有则改成 精准专项
            update['name'] = name.replace("精准脱贫", "精准专项")
            universityScoreRankMean.toUpdateName(update)


def test():
    a = '中央民族大学（少数民族语言）465'
    print(a[-1].isdigit())
    d = a[len(a.rstrip('0123456789')):]
    print(a.split(d)[0])
    print(a[len(a.rstrip('0123456789')):])


# 以university_2为准，批量补全院校地址
def update_address():
    university = University()
    universityScoreRankMean = UniversityScoreRankMean()
    year = 2023
    data = universityScoreRankMean.getList(year)
    for index, row in enumerate(data):
        # if row['province'] == '':
        #     #print(index, row)
        #     # 从院校库获取基础数据
        #     one = university.getOneByCode(row['code'])
        #     if one:
        #         # print('one', one)
        #         update = dict()
        #         update2 = dict()
        #         if one['province_code'] and one['province']:
        #             update['id'] = row['id']
        #             update['province_code'] = one['province_code']
        #             update['province'] = one['province']
        #             print(update)
        #             universityScoreRankMean.toUpdateAddress(update)
        #         if one['city_code'] and one['city']:
        #             update2['id'] = row['id']
        #             update2['city_code'] = one['city_code']
        #             update2['city'] = one['city']
        #             print(update2)
        #             universityScoreRankMean.toUpdateAddress2(update2)
        if row['city_code'] == 0:
            # 从院校库获取基础数据
            one = university.getOneByCode(row['code'])
            if one:
                update2 = dict()
                if one['city_code'] and one['city']:
                    update2['id'] = row['id']
                    update2['city_code'] = one['city_code']
                    update2['city'] = one['city']
                    print(update2)
                    universityScoreRankMean.toUpdateAddress2(update2)


# 以university_2为准，批量转专业难度
def update_change_difficult_level():
    university = University()
    universityScoreRankMean = UniversityScoreRankMean()
    year = 2023
    data = universityScoreRankMean.getList(year)
    for index, row in enumerate(data):
        # 从院校库获取基础数据
        one = university.getOneByCode(row['code'])
        if one:
            update = dict()
            if one['change_difficult_level']:
                update['id'] = row['id']
                update['change_difficult_level'] = one['change_difficult_level']
                print(update)
                universityScoreRankMean.toUpdateChangeDifficultLevel(update)


if __name__ == "__main__":
    update_change_difficult_level()
